<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
  <meta content="width=device-width,initial-scale=1" name="viewport"/>
  <meta content="ie=edge" http-equiv="x-ua-compatible"/>
  <meta content="Copy to clipboard" name="lang:clipboard.copy"/>
  <meta content="Copied to clipboard" name="lang:clipboard.copied"/>
  <meta content="en" name="lang:search.language"/>
  <meta content="True" name="lang:search.pipeline.stopwords"/>
  <meta content="True" name="lang:search.pipeline.trimmer"/>
  <meta content="No matching documents" name="lang:search.result.none"/>
  <meta content="1 matching document" name="lang:search.result.one"/>
  <meta content="# matching documents" name="lang:search.result.other"/>
  <meta content="[\s\-]+" name="lang:search.tokenizer"/>
  <link crossorigin="" href="https://fonts.gstatic.com/" rel="preconnect"/>
  <link href="https://fonts.googleapis.com/css?family=Roboto+Mono:400,500,700|Roboto:300,400,400i,700&amp;display=fallback" rel="stylesheet"/>
  <style>
   body,
      input {
        font-family: "Roboto", "Helvetica Neue", Helvetica, Arial, sans-serif
      }

      code,
      kbd,
      pre {
        font-family: "Roboto Mono", "Courier New", Courier, monospace
      }
  </style>
  <link href="../_static/stylesheets/application.css" rel="stylesheet"/>
  <link href="../_static/stylesheets/application-palette.css" rel="stylesheet"/>
  <link href="../_static/stylesheets/application-fixes.css" rel="stylesheet"/>
  <link href="../_static/fonts/material-icons.css" rel="stylesheet"/>
  <meta content="84bd00" name="theme-color"/>
  <script src="../_static/javascripts/modernizr.js">
  </script>
  <title>
   Writing Converters — TRTorch master documentation
  </title>
  <link href="../_static/material.css" rel="stylesheet" type="text/css"/>
  <link href="../_static/pygments.css" rel="stylesheet" type="text/css"/>
  <link href="../_static/collapsible-lists/css/tree_view.css" rel="stylesheet" type="text/css"/>
  <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js">
  </script>
  <script src="../_static/jquery.js">
  </script>
  <script src="../_static/underscore.js">
  </script>
  <script src="../_static/doctools.js">
  </script>
  <script src="../_static/language_data.js">
  </script>
  <script src="../_static/collapsible-lists/js/CollapsibleLists.compressed.js">
  </script>
  <script src="../_static/collapsible-lists/js/apply-collapsible-lists.js">
  </script>
  <script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js">
  </script>
  <link href="../genindex.html" rel="index" title="Index"/>
  <link href="../search.html" rel="search" title="Search"/>
  <link href="useful_links.html" rel="next" title="Useful Links for TRTorch Development"/>
  <link href="conversion.html" rel="prev" title="Conversion Phase"/>
 </head>
 <body data-md-color-accent="light-green" data-md-color-primary="light-green" dir="ltr">
  <svg class="md-svg">
   <defs data-children-count="0">
    <svg height="448" id="__github" viewbox="0 0 416 448" width="416" xmlns="http://www.w3.org/2000/svg">
     <path d="M160 304q0 10-3.125 20.5t-10.75 19T128 352t-18.125-8.5-10.75-19T96 304t3.125-20.5 10.75-19T128 256t18.125 8.5 10.75 19T160 304zm160 0q0 10-3.125 20.5t-10.75 19T288 352t-18.125-8.5-10.75-19T256 304t3.125-20.5 10.75-19T288 256t18.125 8.5 10.75 19T320 304zm40 0q0-30-17.25-51T296 232q-10.25 0-48.75 5.25Q229.5 240 208 240t-39.25-2.75Q130.75 232 120 232q-29.5 0-46.75 21T56 304q0 22 8 38.375t20.25 25.75 30.5 15 35 7.375 37.25 1.75h42q20.5 0 37.25-1.75t35-7.375 30.5-15 20.25-25.75T360 304zm56-44q0 51.75-15.25 82.75-9.5 19.25-26.375 33.25t-35.25 21.5-42.5 11.875-42.875 5.5T212 416q-19.5 0-35.5-.75t-36.875-3.125-38.125-7.5-34.25-12.875T37 371.5t-21.5-28.75Q0 312 0 260q0-59.25 34-99-6.75-20.5-6.75-42.5 0-29 12.75-54.5 27 0 47.5 9.875t47.25 30.875Q171.5 96 212 96q37 0 70 8 26.25-20.5 46.75-30.25T376 64q12.75 25.5 12.75 54.5 0 21.75-6.75 42 34 40 34 99.5z" fill="currentColor">
     </path>
    </svg>
   </defs>
  </svg>
  <input class="md-toggle" data-md-toggle="drawer" id="__drawer" type="checkbox"/>
  <input class="md-toggle" data-md-toggle="search" id="__search" type="checkbox"/>
  <label class="md-overlay" data-md-component="overlay" for="__drawer">
  </label>
  <a class="md-skip" href="#contributors/writing_converters" tabindex="1">
   Skip to content
  </a>
  <header class="md-header" data-md-component="header">
   <nav class="md-header-nav md-grid">
    <div class="md-flex navheader">
     <div class="md-flex__cell md-flex__cell--shrink">
      <a class="md-header-nav__button md-logo" href="../index.html" title="TRTorch master documentation">
       <i class="md-icon">
        
       </i>
      </a>
     </div>
     <div class="md-flex__cell md-flex__cell--shrink">
      <label class="md-icon md-icon--menu md-header-nav__button" for="__drawer">
      </label>
     </div>
     <div class="md-flex__cell md-flex__cell--stretch">
      <div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
       <span class="md-header-nav__topic">
        TRTorch
       </span>
       <span class="md-header-nav__topic">
        Writing Converters
       </span>
      </div>
     </div>
     <div class="md-flex__cell md-flex__cell--shrink">
      <label class="md-icon md-icon--search md-header-nav__button" for="__search">
      </label>
      <div class="md-search" data-md-component="search" role="dialog">
       <label class="md-search__overlay" for="__search">
       </label>
       <div class="md-search__inner" role="search">
        <form action="../search.html" class="md-search__form" method="GET" name="search">
         <input autocapitalize="off" autocomplete="off" class="md-search__input" data-md-component="query" data-md-state="active" name="q" placeholder="Search" spellcheck="false" type="text"/>
         <label class="md-icon md-search__icon" for="__search">
         </label>
         <button class="md-icon md-search__icon" data-md-component="reset" tabindex="-1" type="reset">
          
         </button>
        </form>
        <div class="md-search__output">
         <div class="md-search__scrollwrap" data-md-scrollfix="">
          <div class="md-search-result" data-md-component="result">
           <div class="md-search-result__meta">
            Type to start searching
           </div>
           <ol class="md-search-result__list">
           </ol>
          </div>
         </div>
        </div>
       </div>
      </div>
     </div>
     <div class="md-flex__cell md-flex__cell--shrink">
      <div class="md-header-nav__source">
       <a class="md-source" data-md-source="github" href="https://github.com/nvidia/TRTorch/" title="Go to repository">
        <div class="md-source__icon">
         <svg height="28" viewbox="0 0 24 24" width="28" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
          <use height="24" width="24" xlink:href="#__github">
          </use>
         </svg>
        </div>
        <div class="md-source__repository">
         TRTorch
        </div>
       </a>
      </div>
     </div>
     <div class="md-flex__cell md-flex__cell--shrink dropdown">
      <button class="dropdownbutton">
       Versions
      </button>
      <div class="dropdown-content md-hero">
       <a href="https://nvidia.github.io/TRTorch/" title="master">
        master
       </a>
       <a href="https://nvidia.github.io/TRTorch/v0.2.0/" title="v0.2.0">
        v0.2.0
       </a>
       <a href="https://nvidia.github.io/TRTorch/v0.1.0/" title="v0.1.0">
        v0.1.0
       </a>
       <a href="https://nvidia.github.io/TRTorch/v0.0.3/" title="v0.0.3">
        v0.0.3
       </a>
       <a href="https://nvidia.github.io/TRTorch/v0.0.2/" title="v0.0.2">
        v0.0.2
       </a>
       <a href="https://nvidia.github.io/TRTorch/v0.0.1/" title="v0.0.1">
        v0.0.1
       </a>
      </div>
     </div>
    </div>
   </nav>
  </header>
  <div class="md-container">
   <nav class="md-tabs" data-md-component="tabs">
    <div class="md-tabs__inner md-grid">
     <ul class="md-tabs__list">
      <li class="md-tabs__item">
       <a class="md-tabs__link" href="../index.html">
        TRTorch master documentation
       </a>
      </li>
     </ul>
    </div>
   </nav>
   <main class="md-main">
    <div class="md-main__inner md-grid" data-md-component="container">
     <div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
      <div class="md-sidebar__scrollwrap">
       <div class="md-sidebar__inner">
        <nav class="md-nav md-nav--primary" data-md-level="0">
         <label class="md-nav__title md-nav__title--site" for="__drawer">
          <a class="md-nav__button md-logo" href="../index.html" title="TRTorch master documentation">
           <i class="md-icon">
            
           </i>
          </a>
          <a href="../index.html" title="TRTorch master documentation">
           TRTorch
          </a>
         </label>
         <div class="md-nav__source">
          <a class="md-source" data-md-source="github" href="https://github.com/nvidia/TRTorch/" title="Go to repository">
           <div class="md-source__icon">
            <svg height="28" viewbox="0 0 24 24" width="28" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
             <use height="24" width="24" xlink:href="#__github">
             </use>
            </svg>
           </div>
           <div class="md-source__repository">
            TRTorch
           </div>
          </a>
         </div>
         <ul class="md-nav__list">
          <li class="md-nav__item">
           <span class="md-nav__link caption">
            <span class="caption-text">
             Getting Started
            </span>
           </span>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../tutorials/installation.html">
            Installation
           </a>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../tutorials/getting_started.html">
            Getting Started
           </a>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../tutorials/ptq.html">
            Post Training Quantization (PTQ)
           </a>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../tutorials/trtorchc.html">
            trtorchc
           </a>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../tutorials/use_from_pytorch.html">
            Using TRTorch Directly From PyTorch
           </a>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../tutorials/runtime.html">
            Deploying TRTorch Programs
           </a>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../tutorials/using_dla.html">
            DLA
           </a>
          </li>
          <li class="md-nav__item">
           <span class="md-nav__link caption">
            <span class="caption-text">
             Notebooks
            </span>
           </span>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../_notebooks/lenet-getting-started.html">
            TRTorch Getting Started - LeNet
           </a>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../_notebooks/ssd-object-detection-demo.html">
            Object Detection with TRTorch (SSD)
           </a>
          </li>
          <li class="md-nav__item">
           <span class="md-nav__link caption">
            <span class="caption-text">
             Python API Documenation
            </span>
           </span>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../py_api/trtorch.html">
            trtorch
           </a>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../py_api/logging.html">
            trtorch.logging
           </a>
          </li>
          <li class="md-nav__item">
           <span class="md-nav__link caption">
            <span class="caption-text">
             C++ API Documenation
            </span>
           </span>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../_cpp_api/trtorch_cpp.html">
            TRTorch C++ API
           </a>
          </li>
          <li class="md-nav__item">
           <span class="md-nav__link caption">
            <span class="caption-text">
             Contributor Documentation
            </span>
           </span>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="system_overview.html">
            System Overview
           </a>
          </li>
          <li class="md-nav__item">
           <input class="md-toggle md-nav__toggle" data-md-toggle="toc" id="__toc" type="checkbox"/>
           <label class="md-nav__link md-nav__link--active" for="__toc">
            Writing Converters
           </label>
           <a class="md-nav__link md-nav__link--active" href="#">
            Writing Converters
           </a>
           <nav class="md-nav md-nav--secondary">
            <label class="md-nav__title" for="__toc">
             Contents
            </label>
            <ul class="md-nav__list" data-md-scrollfix="">
             <li class="md-nav__item">
              <a class="md-nav__link" href="#contributors-writing-converters--page-root">
               Writing Converters
              </a>
              <nav class="md-nav">
               <ul class="md-nav__list">
                <li class="md-nav__item">
                 <a class="md-nav__link" href="#background">
                  Background
                 </a>
                </li>
                <li class="md-nav__item">
                 <a class="md-nav__link" href="#converters">
                  Converters
                 </a>
                </li>
                <li class="md-nav__item">
                 <a class="md-nav__link" href="#converter-contract">
                  Converter Contract
                 </a>
                 <nav class="md-nav">
                  <ul class="md-nav__list">
                   <li class="md-nav__item">
                    <a class="md-nav__link" href="#what-is-guaranteed-to-converters">
                     What is guaranteed to converters
                    </a>
                   </li>
                   <li class="md-nav__item">
                    <a class="md-nav__link" href="#responsibilities-of-a-converter">
                     Responsibilities of a converter
                    </a>
                   </li>
                  </ul>
                 </nav>
                </li>
                <li class="md-nav__item">
                 <a class="md-nav__link" href="#conversion-context">
                  Conversion Context
                 </a>
                </li>
                <li class="md-nav__item">
                 <a class="md-nav__link" href="#args">
                  Args
                 </a>
                </li>
                <li class="md-nav__item">
                 <a class="md-nav__link" href="#weights">
                  Weights
                 </a>
                </li>
                <li class="md-nav__item">
                 <a class="md-nav__link" href="#other-advice">
                  Other advice
                 </a>
                </li>
               </ul>
              </nav>
             </li>
             <li class="md-nav__item">
              <a class="md-nav__extra_link" href="../_sources/contributors/writing_converters.rst.txt">
               Show Source
              </a>
             </li>
            </ul>
           </nav>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="useful_links.html">
            Useful Links for TRTorch Development
           </a>
          </li>
          <li class="md-nav__item">
           <span class="md-nav__link caption">
            <span class="caption-text">
             Indices
            </span>
           </span>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__link" href="../indices/supported_ops.html">
            Operators Supported
           </a>
          </li>
         </ul>
        </nav>
       </div>
      </div>
     </div>
     <div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
      <div class="md-sidebar__scrollwrap">
       <div class="md-sidebar__inner">
        <nav class="md-nav md-nav--secondary">
         <label class="md-nav__title" for="__toc">
          Contents
         </label>
         <ul class="md-nav__list" data-md-scrollfix="">
          <li class="md-nav__item">
           <a class="md-nav__link" href="#contributors-writing-converters--page-root">
            Writing Converters
           </a>
           <nav class="md-nav">
            <ul class="md-nav__list">
             <li class="md-nav__item">
              <a class="md-nav__link" href="#background">
               Background
              </a>
             </li>
             <li class="md-nav__item">
              <a class="md-nav__link" href="#converters">
               Converters
              </a>
             </li>
             <li class="md-nav__item">
              <a class="md-nav__link" href="#converter-contract">
               Converter Contract
              </a>
              <nav class="md-nav">
               <ul class="md-nav__list">
                <li class="md-nav__item">
                 <a class="md-nav__link" href="#what-is-guaranteed-to-converters">
                  What is guaranteed to converters
                 </a>
                </li>
                <li class="md-nav__item">
                 <a class="md-nav__link" href="#responsibilities-of-a-converter">
                  Responsibilities of a converter
                 </a>
                </li>
               </ul>
              </nav>
             </li>
             <li class="md-nav__item">
              <a class="md-nav__link" href="#conversion-context">
               Conversion Context
              </a>
             </li>
             <li class="md-nav__item">
              <a class="md-nav__link" href="#args">
               Args
              </a>
             </li>
             <li class="md-nav__item">
              <a class="md-nav__link" href="#weights">
               Weights
              </a>
             </li>
             <li class="md-nav__item">
              <a class="md-nav__link" href="#other-advice">
               Other advice
              </a>
             </li>
            </ul>
           </nav>
          </li>
          <li class="md-nav__item">
           <a class="md-nav__extra_link" href="../_sources/contributors/writing_converters.rst.txt">
            Show Source
           </a>
          </li>
          <li class="md-nav__item" id="searchbox">
          </li>
         </ul>
        </nav>
       </div>
      </div>
     </div>
     <div class="md-content">
      <article class="md-content__inner md-typeset" role="main">
       <span id="id1">
       </span>
       <h1 id="contributors-writing-converters--page-root">
        Writing Converters
        <a class="headerlink" href="#contributors-writing-converters--page-root" title="Permalink to this headline">
         ¶
        </a>
       </h1>
       <h2 id="background">
        Background
        <a class="headerlink" href="#background" title="Permalink to this headline">
         ¶
        </a>
       </h2>
       <p>
        In the JIT IR, operations are represented as nodes in a graph. A node has inputs and outputs, represented by
        <code class="docutils literal notranslate">
         <span class="pre">
          torch::jit::Values
         </span>
        </code>
        which are typed abstract representation of data flowing into and out of a node. TensorRT represents its graph though the
use of
        <code class="docutils literal notranslate">
         <span class="pre">
          nvinfer1::ILayers
         </span>
        </code>
        and
        <code class="docutils literal notranslate">
         <span class="pre">
          nvinfer1::ITensors
         </span>
        </code>
        which are its analogues to nodes and values. The goal of
converters create new ILayers and subgraphs that do operation specified by the node and associate produced ITensors
and Values together.
       </p>
       <h2 id="converters">
        Converters
        <a class="headerlink" href="#converters" title="Permalink to this headline">
         ¶
        </a>
       </h2>
       <p>
        Converters should be functions which will use a list of inputs (either
        <code class="docutils literal notranslate">
         <span class="pre">
          nvinfer1::ITensors
         </span>
        </code>
        or
        <code class="docutils literal notranslate">
         <span class="pre">
          torch::jit::IValues
         </span>
        </code>
        ) to
construct an equivalent layer to the LibTorch op.
       </p>
       <p>
        Converters can be registered using the
        <code class="docutils literal notranslate">
         <span class="pre">
          RegisterNodeConversionPatterns
         </span>
        </code>
        helper class where you instantiate a
RegisterNodeConversionPatterns object and call the pattern function on it (like below) which takes a string
which describes the function schema of the op that will cause the converter to be run and a lambda or function
which will do the actual conversion:
       </p>
       <blockquote>
        <div>
         <p>
          Note the pattern function can be chained
         </p>
        </div>
       </blockquote>
       <div class="highlight-c++ notranslate">
        <div class="highlight">
         <pre><span></span><span class="k">auto</span> <span class="n">acthardtanh</span> <span class="n">TRTORCH_UNUSED</span> <span class="o">=</span> <span class="n">RegisterNodeConversionPatterns</span><span class="p">()</span>
    <span class="p">.</span><span class="n">pattern</span><span class="p">({</span>
        <span class="s">"aten::hardtanh(Tensor self, Scalar min_val=-1, Scalar max_val=1) -&gt; (Tensor)"</span><span class="p">,</span>
        <span class="p">[](</span><span class="n">ConversionCtx</span><span class="o">*</span> <span class="n">ctx</span><span class="p">,</span> <span class="k">const</span> <span class="n">torch</span><span class="o">::</span><span class="n">jit</span><span class="o">::</span><span class="n">Node</span><span class="o">*</span> <span class="n">n</span><span class="p">,</span> <span class="n">args</span><span class="o">&amp;</span> <span class="n">args</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">bool</span> <span class="p">{</span>
            <span class="k">auto</span> <span class="n">in</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">ITensor</span><span class="p">();</span>
            <span class="k">auto</span> <span class="n">min</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="n">unwrapToDouble</span><span class="p">();</span>
            <span class="k">auto</span> <span class="n">max</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="mi">2</span><span class="p">].</span><span class="n">unwrapToDouble</span><span class="p">();</span>

            <span class="k">auto</span> <span class="n">new_layer</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">-&gt;</span><span class="n">net</span><span class="o">-&gt;</span><span class="n">addActivation</span><span class="p">(</span><span class="o">*</span><span class="n">in</span><span class="p">,</span> <span class="n">nvinfer1</span><span class="o">::</span><span class="n">ActivationType</span><span class="o">::</span><span class="n">kCLIP</span><span class="p">);</span>
            <span class="n">TRTORCH_CHECK</span><span class="p">(</span><span class="n">new_layer</span><span class="p">,</span> <span class="s">"Unable to create layer for aten::hardtanh"</span><span class="p">);</span>

            <span class="n">new_layer</span><span class="o">-&gt;</span><span class="n">setAlpha</span><span class="p">(</span><span class="n">min</span><span class="p">);</span>
            <span class="n">new_layer</span><span class="o">-&gt;</span><span class="n">setBeta</span><span class="p">(</span><span class="n">max</span><span class="p">);</span>

            <span class="n">new_layer</span><span class="o">-&gt;</span><span class="n">setName</span><span class="p">(</span><span class="n">util</span><span class="o">::</span><span class="n">node_info</span><span class="p">(</span><span class="n">n</span><span class="p">).</span><span class="n">c_str</span><span class="p">());</span>
            <span class="k">auto</span> <span class="n">out_tensor</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">-&gt;</span><span class="n">AssociateValueAndTensor</span><span class="p">(</span><span class="n">n</span><span class="o">-&gt;</span><span class="n">outputs</span><span class="p">()[</span><span class="mi">0</span><span class="p">],</span> <span class="n">new_layer</span><span class="o">-&gt;</span><span class="n">getOutput</span><span class="p">(</span><span class="mi">0</span><span class="p">));</span>

            <span class="n">LOG_DEBUG</span><span class="p">(</span><span class="s">"Output shape: "</span> <span class="o">&lt;&lt;</span> <span class="n">out_tensor</span><span class="o">-&gt;</span><span class="n">getDimensions</span><span class="p">());</span>
            <span class="k">return</span> <span class="nb">true</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">});</span>
</pre>
        </div>
       </div>
       <h2 id="converter-contract">
        Converter Contract
        <a class="headerlink" href="#converter-contract" title="Permalink to this headline">
         ¶
        </a>
       </h2>
       <h3 id="what-is-guaranteed-to-converters">
        What is guaranteed to converters
        <a class="headerlink" href="#what-is-guaranteed-to-converters" title="Permalink to this headline">
         ¶
        </a>
       </h3>
       <ol class="arabic simple">
        <li>
         <p>
          In the args there will be an entry for each node input value, either a ITensor or IValue
         </p>
        </li>
        <li>
         <p>
          Inputs will be provided in order according to the function schema
         </p>
        </li>
       </ol>
       <h3 id="responsibilities-of-a-converter">
        Responsibilities of a converter
        <a class="headerlink" href="#responsibilities-of-a-converter" title="Permalink to this headline">
         ¶
        </a>
       </h3>
       <ol class="arabic simple">
        <li>
         <p>
          Args must be guaranteed to be a type to unwrap the Arg union without checking, typically input tensor arguments can be expected to be ITensors
         </p>
        </li>
        <li>
         <p>
          Any weights or static values must guaranteed to be valid until the end of conversion time
         </p>
         <ol class="loweralpha simple">
          <li>
           <p>
            A helpful tool is the Weights helper class described below
           </p>
          </li>
         </ol>
        </li>
        <li>
         <p>
          Converters are expected to produce an IValue or ITensor for each output of a node. The compiler will check this and produce warnings if there are Values that don’t have associated ITensors or IValues.
         </p>
        </li>
        <li>
         <p>
          Outputs must be annotated
         </p>
         <ol class="loweralpha simple">
          <li>
           <p>
            There must be an association between a JIT nodes output values and the new TRT layers output tensors in the
            <code class="docutils literal notranslate">
             <span class="pre">
              value_tensor_map
             </span>
            </code>
            in the conversion context
           </p>
          </li>
         </ol>
        </li>
        <li>
         <p>
          Name your layers
         </p>
         <ol class="loweralpha simple">
          <li>
           <p>
            Its much easier to debug when we can track which layers and nodes correspond with each other. The system we are currently using is to use the “node info” of the node as the name of the layer
           </p>
          </li>
         </ol>
        </li>
        <li>
         <p>
          Name your tensors
         </p>
         <ol class="loweralpha simple">
          <li>
           <p>
            Use the output value debug name as the name for the new ITensor (again for debugging)
           </p>
          </li>
         </ol>
        </li>
       </ol>
       <h2 id="conversion-context">
        Conversion Context
        <a class="headerlink" href="#conversion-context" title="Permalink to this headline">
         ¶
        </a>
       </h2>
       <p>
        The conversion context maintains the state of conversion, it manages the Network Definition, two maps
one that stores associations between Values and IValues (the evaluated_value_map) and one that stores
associations between Values and ITensors, and any sort of memory that needs to live until the end of
conversion. The main apis that you will interface with in converters is directly accessing the network
definition to add layers
        <code class="docutils literal notranslate">
         <span class="pre">
          ctx-&gt;net
         </span>
        </code>
        and data association functions
        <code class="docutils literal notranslate">
         <span class="pre">
          ctx-&gt;AssociateValueAndTensor()
         </span>
        </code>
        and
        <code class="docutils literal notranslate">
         <span class="pre">
          ctx-&gt;AssociateValueAndIValue()
         </span>
        </code>
        , which you will use to add layers to the TRT layers and log
pairs of node outputs and static values or TensorRT layer outputs.
       </p>
       <h2 id="args">
        Args
        <a class="headerlink" href="#args" title="Permalink to this headline">
         ¶
        </a>
       </h2>
       <p>
        Arguments provided to the converter are inspectable unions of
        <code class="docutils literal notranslate">
         <span class="pre">
          nvinfer1::ITensors
         </span>
        </code>
        and
        <code class="docutils literal notranslate">
         <span class="pre">
          torch::jit::IValues
         </span>
        </code>
        (i.e.
abstract dataflow in the TensorRT graph and static values). You are guaranteed that you will have some
argument for each input value for the node. They are provided in the order of the function schema.
It can be expected that inputs (meaning the parameters that would be passed into the forward
function of a module in PyTorch) will be ITensors but the Arg class also has mechanisms to inspect arguments safely
before unwrapping if you are unsure. Args also have deep unwrap methods that let you get straight to the
underlying data in an IValue if you know it’s safe. You can also pass in a fallback value if there is a
chance the IValue is None. IValues have been extended to be able to hold a wrapper around ITensors only in the case of TensorLists.
You can get an ITensor from an IValue by a pattern similar to this:
        <code class="docutils literal notranslate">
         <span class="pre">
          ivalue.toCustomClass&lt;TensorContainer&gt;()-&gt;tensor()
         </span>
        </code>
        .
You can tell if an IValue contains a Tensor or an ITensor by using
        <code class="docutils literal notranslate">
         <span class="pre">
          ivalue.isTensor()
         </span>
        </code>
        or
        <code class="docutils literal notranslate">
         <span class="pre">
          ivalue.isCustomClass()
         </span>
        </code>
        .
       </p>
       <h2 id="weights">
        Weights
        <a class="headerlink" href="#weights" title="Permalink to this headline">
         ¶
        </a>
       </h2>
       <p>
        Weights are used during build time, so any weights need to be guaranteed to live until the end of the conversion phase.
TensorRT also uses its own weights structure to hold the weights. There is a wrapper around this class available
to converts which abstracts a lot of this.
       </p>
       <p>
        The weights wrapper class can accept either
        <code class="docutils literal notranslate">
         <span class="pre">
          at::Tensors
         </span>
        </code>
        or singular values (right now). You also need to pass the
conversion context when constructing these weights because internally the weights class will allocate memory managed
by the conversion context to store a copy of the tensor data. This data gets freed when the conversion context
destructor gets destroyed so converters don’t really need to think about it.
       </p>
       <p>
        There is metadata generated from the shape of the input data which becomes useful in interfacing with TensorRT, such
as number of input maps, number of output maps and kernel shape.
       </p>
       <h2 id="other-advice">
        Other advice
        <a class="headerlink" href="#other-advice" title="Permalink to this headline">
         ¶
        </a>
       </h2>
       <p>
        You have the benefit of the full aten library when dealing with weights and other static values. This means that you
can do quite a bit of work during conversion time to produce efficient conversion. A good example is batch_norm
converter where the converter does fusion of operations with PyTorch before creating the TensorRT layer.
       </p>
      </article>
     </div>
    </div>
   </main>
  </div>
  <footer class="md-footer">
   <div class="md-footer-nav">
    <nav class="md-footer-nav__inner md-grid">
     <a class="md-flex md-footer-nav__link md-footer-nav__link--prev" href="conversion.html" rel="prev" title="Conversion Phase">
      <div class="md-flex__cell md-flex__cell--shrink">
       <i class="md-icon md-icon--arrow-back md-footer-nav__button">
       </i>
      </div>
      <div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
       <span class="md-flex__ellipsis">
        <span class="md-footer-nav__direction">
         Previous
        </span>
        Conversion Phase
       </span>
      </div>
     </a>
     <a class="md-flex md-footer-nav__link md-footer-nav__link--next" href="useful_links.html" rel="next" title="Useful Links for TRTorch Development">
      <div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
       <span class="md-flex__ellipsis">
        <span class="md-footer-nav__direction">
         Next
        </span>
        Useful Links for TRTorch Development
       </span>
      </div>
      <div class="md-flex__cell md-flex__cell--shrink">
       <i class="md-icon md-icon--arrow-forward md-footer-nav__button">
       </i>
      </div>
     </a>
    </nav>
   </div>
   <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
     <div class="md-footer-copyright">
      <div class="md-footer-copyright__highlight">
       © Copyright 2020, NVIDIA Corporation.
      </div>
      Created using
      <a href="http://www.sphinx-doc.org/">
       Sphinx
      </a>
      3.1.2.
             and
      <a href="https://github.com/bashtage/sphinx-material/">
       Material for
              Sphinx
      </a>
     </div>
    </div>
   </div>
  </footer>
  <script src="../_static/javascripts/application.js">
  </script>
  <script>
   app.initialize({version: "1.0.4", url: {base: ".."}})
  </script>
 </body>
</html>